module OmgroflTranslators where

-- | Omgrofl to Mid-level C-like lang tranlator
omgroflToMidLevel :: String -> [String]
omgroflToMidLevel txt = map (omgroflToMidLevelString) $ lines txt

-- | Translates an omfgrofl operation into a Mid-level C-like instruction
omgroflToMidLevelString :: String -> String
omgroflToMidLevelString a
        -- Variables
        | length w >= 3 -- "#variable_name iz #number"
                && w !! 1 == "iz"
                        = (!!) w 0 ++ " = " ++ (!!) w 2
        -- Conditionals
        | length w >= 5 -- "wtf #variable_name iz liek #number"
                && elem "liek" w
                        = "if " ++ (!!) w 1 ++ " == " ++ (!!) w 4
        | length w >= 5 -- "wtf #variable_name iz uber #number"
                && elem "uber" w
                        = "if " ++ (!!) w 1 ++ " > " ++ (!!) w 4
        | length w >= 6 -- "wtf #variable_name iz nope liek #number"
                && elem "nope" w
                && elem "liek" w
                        = "if " ++ (!!) w 1 ++ " != " ++ (!!) w 5
        | length w >= 6 -- "wtf #variable_name iz uber #number"
                && elem "nope" w
                && elem "uber" w
                        = "if " ++ (!!) w 1 ++ " !> " ++ (!!) w 5
        -- Loops
        | length w >= 1 && elem "rtfm" w        -- Infinite "for" loop
                = "for"
        | length w >= 1 && elem "brb" w         -- loop end
                = "end"
        | length w >= 1  && elem "tldr" w       -- "if" break
                = "break"
        | length w >= 6 && elem "4" w           -- Classic "for" loop
                = "for ( " ++ (!!) w 1 ++ " = " ++
                        (!!) w 3 ++ " : " ++ (!!) w 5 ++ " )"
        -- I/O
        | length w >= 2 && elem "stfw" w -- "stfw #variable_name"
                = "read " ++ (!!) w 1
        | length w >= 2 && elem "rofl" w -- "rofl #variable_name"
                = "print " ++ (!!) w 1
        -- Stack/Queue
        | length w >= 2 && elem "n00b" w -- "n00b #variable_name"
                = "push " ++ (!!) w 1
        | length w >= 2 && elem "l33t" w -- "l33t #variable_name"
                = "pop " ++ (!!) w 1
        | length w >= 2 && elem "haxor" w -- "haxor #variable_name"
                = "dequeue " ++ (!!) w 1
        -- Variable Manipulation
        | length w >= 2 && elem "lmao" w -- "lmao #variable_name"
                = (!!) w 1 ++ " ++"
        | length w >= 2 && elem "roflmao" w -- "roflmao #variable_name"
                = (!!) w 1 ++ " --"
        -- Misc
        | length w >= 1 && elem "stfu" w
                = "exit"
        | length w >= 2 -- "afk #number"
           && elem "afk" w
                = "pause " ++ (!!) w 1
        | otherwise = ""
        where w = words a
